#include <QtGui>

#include "renderarea.h"
#include "juliathread.h"
#include "juliawindow.h"

JuliaWindow::JuliaWindow(QWidget *parent)
    : QWidget(parent)
{
    renderArea = new RenderArea(new JuliaThread(this));

    iterationSpinBox = new QSpinBox;
    iterationSpinBox->setRange(0, 1000);

    iterationLabel = new QLabel(tr("&Iterations:"));
    iterationLabel->setBuddy(iterationSpinBox);

    lambdaReSpinBox = new QDoubleSpinBox;
    lambdaReSpinBox->setRange(-std::numeric_limits<double>::max(), std::numeric_limits<double>::max());
    lambdaReSpinBox->setSingleStep(0.01);
    lambdaReSpinBox->setValue(-0.5);

    lambdaReLabel = new QLabel(tr("&Lambda:"));
    lambdaReLabel->setBuddy(lambdaReSpinBox);

    lambdaImSpinBox = new QDoubleSpinBox;
    lambdaImSpinBox->setRange(-std::numeric_limits<double>::max(), std::numeric_limits<double>::max());
    lambdaImSpinBox->setSingleStep(0.01);
    lambdaImSpinBox->setSuffix("i");
    lambdaImSpinBox->setValue(0.5);

    lambdaImLabel = new QLabel(tr(" + "));
    lambdaImLabel->setBuddy(lambdaImSpinBox);

    helpLabel = new QLabel("Use mouse wheel or the '+' and '-' keys to zoom.\n"
                           "Press and hold left mouse button to scroll.");
    resetButton = new QPushButton(tr("&Reset"));
    quitButton = new QPushButton(tr("&Quit"));

    connect(iterationSpinBox, SIGNAL(valueChanged(int)),
            renderArea, SLOT(setIteration(int)));
    connect(resetButton, SIGNAL(clicked()),
            renderArea, SLOT(reset()));
    connect(quitButton, SIGNAL(clicked()),
            qApp, SLOT(quit()));
    connect(lambdaReSpinBox, SIGNAL(valueChanged(double)),
            this, SLOT(lambdaChanged()));
    connect(lambdaImSpinBox, SIGNAL(valueChanged(double)),
            this, SLOT(lambdaChanged()));

    QHBoxLayout *topLayout = new QHBoxLayout;
    topLayout->addWidget(iterationLabel);
    topLayout->addWidget(iterationSpinBox);
    topLayout->addStretch();
    topLayout->addWidget(lambdaReLabel);
    topLayout->addWidget(lambdaReSpinBox);
    topLayout->addWidget(lambdaImLabel, 0, Qt::AlignCenter);
    topLayout->addWidget(lambdaImSpinBox);
    
    QHBoxLayout *bottomLayout = new QHBoxLayout;
    bottomLayout->addWidget(helpLabel, 1);
    bottomLayout->addWidget(resetButton);
    bottomLayout->addWidget(quitButton);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(topLayout);
    mainLayout->addSpacing(6);
    mainLayout->addWidget(renderArea);
    mainLayout->addSpacing(6);
    mainLayout->addLayout(bottomLayout);

    setLayout(mainLayout);

    iterationSpinBox->setValue(20);
    lambdaChanged();

    setWindowTitle(tr("Julia"));
}

void JuliaWindow::setLambda(double x, double y)
{
    lambdaReSpinBox->setValue(x);
    lambdaImSpinBox->setValue(y);
    renderArea->setLambda(x, y);
    lambdaChanged();
}

void JuliaWindow::lambdaChanged()
{
    Complex lambda(lambdaReSpinBox->value(), lambdaImSpinBox->value());
    if (lambda == renderArea->lambda())
        return;

    renderArea->setLambda(lambdaReSpinBox->value(), lambdaImSpinBox->value());
}
